## Project Makefile
## Ratish J. Punnoose
## Note: Makefile requires GNU Make 3.81 or higher
## --------------------------------------------------
## What type of release is this.
#
# Instructions to override defaults from your project makefile
# --------------------------------------------------
# To override the platform, specify 
# PLATFORM = your_platform_name
# where your_platform_name is one of posix, msp430gcc
#
# To override the default platform specific source files added by this
# makefile, specify
# USER_SOURCES_ONLY = x 
# The value of x doesn't matter.  Only the existence of the variable
# is checked.
# --------------------------------------------------


DEFAULT_PLATFORM = posix
#DEFAULT_PLATFORM = msp430gcc

DEFAULT_MSP430JTAGOPTS = --lpt=TIUSB

ifndef PLATFORM
  PLATFORM = $(DEFAULT_PLATFORM)
endif


ifndef MSP430JTAGOPTS
  MSP430JTAGOPTS = ${DEFAULT_MSP430JTAGOPTS}
endif



ifeq ($(PLATFORM),msp430gcc)
  INCDIRS         += $(MDIR)/src/tiros/port/msp430_gcc \
                     $(MDIR)/src/tiros
  TSRCS		=  $(PROJDIR)/platform_msp430.c  \
		  $(MDIR)/src/tiros/port/msp430_gcc/tr_port.c \
	       	  $(MDIR)/src/util/msp430_src/delay.c \
		  $(MDIR)/src/util/msp430_src/putchar.c \
		  $(MDIR)/src/util/msp430_src/usart/msp430_usart.c 
  CPU             = msp430x1611
#  CPU             = msp430x2618
  CC              = msp430-gcc
  #	 GCC alignment issues for MSP430 
  #	"http://sourceforge.net/mailarchive/forum.php?thread_id=3871218&forum_id=7539"
  #	 Use option -mstrict-align 
  #      Essential to get radio working.
  INCFLAGS	+= $(addprefix -I, $(INCDIRS))
#  CFLAGS          += -mmcu=${CPU} -O4 -mstrict-align -Wall -W -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 
   CFLAGS          += -mmcu=${CPU} -O2 -mstrict-align -Wall  -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 
#  CFLAGS          += -mmcu=${CPU} -O0 -mstrict-align -Wall  -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 

#  CFLAGS      += -ffunction-sections
#  LDFLAGS     += -Wl,-gc-sections
#  CFLAGS          += -mmcu=${CPU} -O4 -Wall -W -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 
#  CFLAGS          += -mmcu=${CPU} -O2 -Wall -W -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 
#   CFLAGS          += -mmcu=${CPU} -O0 -Wall -W -Wbad-function-cast -g -D$(TYPE)	$(INCFLAGS) 

#  CFLAGS          += -mmcu=${CPU} -O2 -mstrict-align -Wall -g -D$(TYPE)	$(INCFLAGS) 
  #CFLAGS          += -mmcu=${CPU} -O2 -Wall -g -D$(TYPE)	$(INCFLAGS) 
  #LIBS		= -lmspgcc
  #LIBS		= -lmspgcc
else ifeq ($(PLATFORM),posix)
  INCDIRS         += $(MDIR)/src/tiros/port/posix
  TSRCS		=  $(PROJDIR)/platform_posix.c  \
		  $(MDIR)/src/tiros/port/posix/tr_port.c \
		  $(MDIR)/src/util/posix_src/putchar.c  \
		  $(MDIR)/src/util/posix_src/delay.c
  CC              = gcc
  INCFLAGS	+= $(addprefix -I, $(INCDIRS))
  sysname = $(shell uname)
  CFLAGS          +=  -O0  -Wall  -g -D$(TYPE)   $(INCFLAGS) 
  ##--ifeq  (,$(findstring CYGWIN,$(sysname)))
  ifeq (Linux,$(findstring Linux, $(sysname)))
    # System is Linux
    LIBS = -lrt
  else
    # Cygwin, MAC OS X
    LIBS		= 
  endif
endif

ifndef USER_SOURCES_ONLY
SRCS += $(TSRCS)
endif

#LDFLAGS		+= $(LIBS) -nodefaultlibs
LDFLAGS		+= $(LIBS)

$(info CFLAGS=$(CFLAGS))


ifneq (, $(filter $(OBJDIR),$(notdir $(CURDIR))))



## Don't need to edit below this line
## ------------------------------------------------------------
# Makes a list of the object files that will have to be created.


#VPATH =  $(addprefix $(PROJDIR)/, $(dir $(SRCS)) )
VPATH =  $(dir $(SRCS))

OBJECTS := $(notdir $(SRCS:.c=.o))
#FULLSRCS := $(addprefix ../, $(SRCS))
FULLSRCS :=  $(SRCS)
ASMFILES := $(notdir $(SRCS:.c=.s))
PPFILES := $(notdir $(SRCS:.c=.e))


ifdef OBJ_FILES_ONLY
default_target: $(OBJECTS)
else
default_target: all
endif


ifeq ($(PLATFORM),msp430gcc)
all: ${NAME}.elf ${NAME}.lst
else
all: ${NAME}
endif



.PHONY: all FORCE clean veryclean download download-jtag download-bsl dist



#confgigure the next line if you want to use the serial download
download: download-jtag

assembly: $(ASMFILES)

preprocess: $(PPFILES)

${NAME} : ${OBJECTS}
	${CC} -o $@ ${OBJECTS} ${LDFLAGS}

#additional rules for files
${NAME}.elf: ${OBJECTS}
	${CC} -mmcu=${CPU}  -o $@ ${OBJECTS} $(LDFLAGS)

${NAME}.a43: ${NAME}.elf
	msp430-objcopy -O ihex $^ $@

${NAME}.lst: ${NAME}.elf
	@msp430-objdump -dSt $^ >$@

download-jtag: all
	msp430-jtag ${MSP430JTAGOPTS} -e ${NAME}.elf

download-bsl: all
	msp430-bsl -e ${NAME}.elf

dummy_debug:
	@-printf "\033[1;33m MYTST = $(MYTST) TSTMDIR = $(TSTMDIR) CURDIR = $(CURDIR) PROJDIR=$(PROJDIR)\033[0m  \n"

# General compilation rule
%.o: %.c 
	@-printf "$(notdir $<) \033[1;33m --> \033[0m $@ \n"
	@$(CC) $(CFLAGS) $(foreach INC,$(INCPATH),-I$(INC))  $(foreach MACRO,$(MACROS),-D$(MACRO)) -c $< -o $@

%.s: %.c 
	@-printf "$(notdir $<) \033[1;33m --> \033[0m $@ \n"
	@$(CC) -S $(CFLAGS) $(foreach INC,$(INCPATH),-I$(INC))  $(foreach MACRO,$(MACROS),-D$(MACRO)) -c $< -o $@


%.e: %.c 
	@-printf "$(notdir $<) \033[1;33m --> \033[0m $@ \n"
	@$(CC) -E $(CFLAGS) $(foreach INC,$(INCPATH),-I$(INC))  $(foreach MACRO,$(MACROS),-D$(MACRO)) -c $< -o $@


depend: .depend

.depend: $(FULLSRCS)
	@echo "Making depend with $(FULLSRCS)"
	@$(CC) -E -MM $(CFLAGS) $(FULLSRCS)   > .depend;
#	@$(CC) -E -MM $(CFLAGS) $(FULLSRCS) | sed "s,^[[:graph:]],$(STORE)/&,"   > .depend;


include .depend



else

# Copied from make info on multi-arch builds http://make.paulandlesley.org/multi-arch.html

#MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile PROJDIR=$(CURDIR) $(MAKECMDGOALS)
# Use relative paths instead of absolute paths 
MAKETARGET = $(MAKE) --no-print-directory -C $@ -f $(CURDIR)/Makefile PROJDIR=.. $(MAKECMDGOALS)
.PHONY: $(OBJDIR) clean veryclean

$(OBJDIR):
	+@[ -d $@ ] || mkdir -p $@
	+@$(MAKETARGET)

Makefile : ;
%.mk :: ;
% :: $(OBJDIR) ; @:

clean:
	rm -rf $(OBJDIR); 

veryclean: clean
	rm -rf *~ Debug settings  project.lnt

dummy_debug2:
	@-printf "\033[1;33m MYTST = $(MYTST) TSTMDIR = $(TSTMDIR) MDIR = $(MDIR) CURDIR = $(CURDIR) PROJDIR=$(PROJDIR)\033[0m  \n"
	@-printf "\033[1;33m SRCS=$(SRCS)\033[0m  \n"


# Export list of files for linting
project.lnt:
	@-printf "/* Include the main directory */\n\n" > project.lnt
	@-printf " --i\"$(MDIR)\"\n\n" >> project.lnt
	@-printf "$(MDIR)/workspace.lnt\n\n" >> project.lnt
	@-printf " -d$(TYPE)\n" >> project.lnt
	@-printf "/* Add Include Directories */\n" >> project.lnt
	@-printf " $(addprefix --i\", $(addsuffix \"\n, $(INCDIRS)))\n" >> project.lnt
	@-printf "/* Add Sources */\n" >> project.lnt
	@-printf "$(addsuffix  \n, $(SRCS))\n" >> project.lnt


lint: project.lnt
	pclint project.lnt

endif



